<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">进行考试</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>进行考试</cite></a>
        </span>
    </div>
<!--    <div class="layui-card-body">-->
<!--        <div class="layui-form toolbar">-->
<!--            考卷名：-->
<!--            <input id="examTitle" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;-->
<!--            阶段：-->
<!--            <select id="examStage">-->
<!--                <option value="">-请选择-</option>-->
<!--            </select>&emsp;-->
<!--            <button id="exam-paper-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>-->
<!--            <button id="exam-paper-btn-add" class="layui-btn icon-btn">考试统计</button>-->
<!--        </div>-->
<!--    </div>-->
    <div class="layui-card-body">
        <table class="layui-table" id="exam-table" lay-filter="exam-filter"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="exam-table-bar">
    <a class="layui-btn layui-btn-normal  layui-btn-xs " lay-event="exam-start">开始考试</a>
    <a class="layui-btn layui-btn-primary  layui-btn-xs " lay-event="exam-look">查看结果</a>
</script>


<script>
    layui.use(['index', 'form', 'table', 'util', 'config', 'admin', 'layer'], function () {
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var index = layui.index;
        var layer = layui.layer;
        var form = layui.form;
        let userid;
        index.getUser(function (user) {
            userid = user.id
        });
        // 渲染表格
        table.render({
            elem: '#exam-table',
            url: config.base_server + 'api-exam/exam/findExam',
            method: 'get',
            where: {
                access_token: config.getToken().access_token,
                userid: userid
            },
            page: true,
            height: 'full-350',
            cols: [[
                {type: 'numbers'},
                {field: 'epname', title: '试卷名'},
                {field: 'remark', title: '简介要求'},
                {field: 'nickName', width: 120, title: '创建人'},
                {field: 'cretim', width: 120, title: '创建时间'},
                {
                    field: 'status', width: 100, title: '状态',
                    templet: function (data) {
                        let param = {expaid: data.expaid, userid: userid};
                        let result = getStatus(param);
                        if (result[0].status === 1) {
                            return '<span class="layui-badge layui-bg-gray">未参考</span>';
                        } else if (result[0].status === 2) {
                            return '<span class="layui-badge layui-bg-green">已交卷</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-gray">已完成</span>';
                        }
                    }
                },
                {align: 'center', toolbar: '#exam-table-bar', title: '操作', width: 250}
            ]],
            limit: 10,
        });

        table.on('tool(exam-filter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'exam-start') { // 考试
                examStart(data)
            }
            if (layEvent === 'exam-look') { // 查看答案
                examLook(data)
            }
        });

        //考试
        function examStart(data) {
            let param = {userid: userid, expaid: data.expaid};
            let res = getStatus(param);
            if (res[0].status != 1) {
                layer.msg("不能重复考试")
                return false;
            }
            layer.confirm("是否进行考试", function (index) {
                admin.putTempData("paper_data", data);
                admin.popupCenter({
                    title: data.epname,
                    area: ['40%', '75%'],
                    path: 'pages/exam/exam-start.html',
                    success: function (layero) {
                        $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                        layer.close(index)
                    },
                    finish: function () {
                        table.reload('exam-table');
                    }
                });
            })
        }

        //查看答案
        function examLook(data) {
            let param = {userid: userid, expaid: data.expaid};
            let res = getStatus(param);
            if (res[0].status == 1) {
                layer.msg("请先进行考试")
                return false;
            }
            data.userid=userid;
            admin.putTempData("paper_data", data);
            admin.popupCenter({
                title: '试卷：' + data.epname,
                area: ['50%', '75%'],
                path: 'pages/exam/exam-result.html',
                success: function (layero) {
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                },
                finish: function () {
                    table.reload('exam-table');
                }
            });
        }

        //条件搜索
        $("#exam-paper-btn-search").click(function () {
            // let epname = $("#ex").val();
            // let grouid = $("#exam-paper-group").val();
            // let param = {epname: epname, grouid: grouid};
            // table.reload('exam-paper', {where: param});
        });

        //获取考试状态
        function getStatus(param) {
            let result;
            admin.req("api-exam/exam/findAnswer", param, function (res) {
                result = res.data;
            }, "get");
            return result;
        }
    });


</script>